#include<bits/stdc++.h>
#define ll long long
#define db double
#define Si set<int>::iterator
using namespace std;
int const N=1e6+5,M=17;
ll read()
{
    char x=getchar();
    ll s=0,f=1;
    while(x<'0'||x>'9')
    {
        if(x=='-')f=-f;
        x=getchar();
    }
    while('0'<=x&&x<='9')s=s*10+x-'0',x=getchar();
    return s*f;
}
bool isbit[N];
void init()
{
    int i,j;
    isbit[j=1]=1;
    for(i=1;i<=M;i++)
    {
        j*=2;isbit[j]=1;
    }
}
struct query
{
    int c,rk;
}q[N];
int raw[N],X[4],a[N],id[N];
char D[M][N];
bool cmp(query a,query b)
{
    return a.c<b.c;
}
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);/**/


    //ll!!!


    init();
    int n,m,i,j,T,K,k,l;
    bool flg1=1,flg2=1;
    n=read();m=read();
    K=1.0*log(n)/log(2);
    if((1<<K)!=n)K++;
    for(i=1;i<=n;i++)
    {
        raw[i]=read();
    }
    for(i=1;i<=m;i++)
    {
        q[i].c=read();q[i].rk=i;
        if(!isbit[q[i].c])flg1=0;
    }
    sort(q+1,q+m+1,cmp);
    for(i=1;i<=K;i++)
    {
        scanf("%s",D[i]+1);
        for(j=1;j<=(1<<(K-i));j++)
        {
            if(D[i][j]=='1')flg2=0;
        }
    }
    T=read();
    while(T--)
    {
        ll ans=0;
        for(i=0;i<4;i++)X[i]=read();
        if(flg1)
        {
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=n;j++)
                {
                    a[j]=raw[j]^X[j%4];
                    id[j]=j;
                }
                int KK=1.0*log(q[i].c)/log(2);
                for(k=1;k<=KK;k++)
                {
                    for(j=1;j<=(1<<(KK-k+1));j+=2)
                    {
                        int p=id[(D[k][j/2+1]=='0'?j:j+1)];
                        if(a[p]>=k)id[j/2+1]=p;
                        else id[j/2+1]=(p==id[j]?id[j+1]:id[j]);
                    }
                }
                ans^=(1ll*q[i].rk*id[1]);
                while(q[i].c==q[i+1].c)
                {
                    i++;
                    ans^=(1ll*q[i].rk*id[1]);
                }
            }
            printf("%lld\n",ans);
        }
        if(flg2)
        {
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=n;j++)
                {
                    a[j]=raw[j]^X[j%4];
                    id[j]=j;
                }
                int KK=1.0*log(q[i].c)/log(2);
                for(k=1;k<=KK;k++)
                {
                    for(j=1;j<=(1<<(KK-k+1));j+=2)
                    {
                        int p=id[(D[k][j/2+1]=='0'?j:j+1)];
                        if(a[p]>=k)id[j/2+1]=p;
                        else id[j/2+1]=(p==id[j]?id[j+1]:id[j]);
                    }
                }
                if((1<<KK)!=q[i].c)KK++;
                KK=(1<<KK);
                if(a[id[1]]>=k)ans^=(1ll*q[i].rk*id[1]);
                else ans^=1ll*q[i].rk*(q[i].c+1+KK)*(KK-q[i].c)/2;
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}
